<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body {
      height: 2000px;
      margin: 0;
      padding: 0;
    }

   .sidebar {
      position: sticky;
      top: 0;
      height: 200px;
      width: 200px;
      background-color: lightgray;
    }

   .back-to-top {
      display: none;
      position: fixed;
      bottom: 20px;
      right: 20px;
      width: 50px;
      height: 50px;
      background-color: blue;
      color: white;
      text-align: center;
      line-height: 50px;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="sidebar"></div>
  <div class="back-to-top">回到顶部</div>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    $(window).on('scroll', function () {
      var sidebar = $('.sidebar');
      sidebar.css('top', Math.max(0, $(this).scrollTop()));

      if ($(this).scrollTop() > 200) {
        $('.back-to-top').fadeIn();
      } else {
        $('.back-to-top').fadeOut();
      }
    });

    $('.back-to-top').on('click', function () {
      $('html, body').animate({
        scrollTop: 0
      }, 'slow');
    });
  </script>
</body>

</html>